package com.acme.bankapp.service.email;

import java.io.*;

/**
 * Class provides email sender functionality
 * @author alex
 */
@SuppressWarnings("serial")
public class EmailSender extends Thread implements Serializable {
	private Queue queue;
	private boolean closed;
	
	/**
	 * The default constructor
	 */
	public EmailSender() {
		this.queue = new Queue();
	}
	
	/**
	 * @return the queue
	 */
	public Queue getQueue() {
		return this.queue;
	}
	
	/**
	 * Run email sender
	 */
	@Override
	public void run() {
		while (!this.closed || !this.queue.isEmpty()) {
			if (!this.queue.isEmpty()) {
				System.out.println("* Email for " + queue.get());
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				System.out.println("Err: Interrupted exception!");
			}
		}
	}
	
	/**
	 * Close email sender
	 */
	public void close() {
		this.closed = true;
	}
}
